/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmEmisorServidor.java
 *
 * Created on 12/06/2011, 09:29:06 PM
 */

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author Mario
 */
public class frmEmisorServidor extends javax.swing.JFrame {

    private rmiEmisorServidor rmiES;
    private boolean validarjTIp = false;
    private validarDireccionIp validarDireccionIp;
    private String estaIP;

    Thread verificarjTIp = new Thread(new Runnable() {
            public void run() {
                while(true){
                    validarjTIp = validarDireccionIp.validar(jTIp.getText());
                    if (!validarjTIp){
                        jBConectar.setEnabled(false);
                        jLIconoValidador.setIcon(new ImageIcon(getClass().getResource("/bullet_cross.png")));
                        jLIconoValidador.setToolTipText("Dirección ip no valida.");                        
                    }else{
                        jBConectar.setEnabled(true);
                        jLIconoValidador.setIcon(new ImageIcon(getClass().getResource("/bullet_tick.png")));
                        jLIconoValidador.setToolTipText("Dirección ip valida.");
                    }
                }
            }
        });

    /** Creates new form frmEmisorServidor */
    public frmEmisorServidor() {
        try {
            initComponents();
            verificarjTIp.start();
            estaIP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException ex) {
            Logger.getLogger(frmEmisorServidor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLImagenIp = new javax.swing.JLabel();
        jLDescripcion = new javax.swing.JLabel();
        jLIp = new javax.swing.JLabel();
        jTIp = new javax.swing.JTextField();
        jBConectar = new javax.swing.JButton();
        jBSalir = new javax.swing.JButton();
        jLConeccion = new javax.swing.JLabel();
        jLIconoValidador = new javax.swing.JLabel();
        jLEjemploIp = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLImagenIp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imgIp.png"))); // NOI18N

        jLDescripcion.setText("<html>Escriba la dirección ip valida del servidor en donde se instalo la Consola de Control de Operaciones (CCO).</html> ");

        jLIp.setText("Dirección ip:");

        jTIp.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTIp.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jBConectar.setText("Conectar");
        jBConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBConectarActionPerformed(evt);
            }
        });

        jBSalir.setText("Salir");
        jBSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalirActionPerformed(evt);
            }
        });

        jLConeccion.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLConeccion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/3.png"))); // NOI18N
        jLConeccion.setText("No Conectado");

        jLIconoValidador.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bullet_cross.png"))); // NOI18N

        jLEjemploIp.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLEjemploIp.setText("Ej. 192.168.55.25");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jLIp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(26, 26, 26)
                .addComponent(jLIconoValidador, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTIp, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLImagenIp, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(276, 276, 276))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLConeccion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                .addComponent(jBConectar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBSalir)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(250, Short.MAX_VALUE)
                .addComponent(jLEjemploIp)
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap(69, Short.MAX_VALUE)
                    .addComponent(jLDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLImagenIp, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLIp)
                    .addComponent(jTIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLIconoValidador))
                .addGap(4, 4, 4)
                .addComponent(jLEjemploIp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLConeccion, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBSalir)
                    .addComponent(jBConectar)))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(109, Short.MAX_VALUE)))
        );

        jLConeccion.setToolTipText("Sin conección con el servidor del CCO.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBConectarActionPerformed
        // TODO add your handling code here:        
        jBConectar.setEnabled(false);
        jBSalir.setEnabled(false);
        jTIp.setEditable(false);        
        cPing ping = new cPing();
        boolean bool = ping.hacerping(jTIp.getText());
        System.out.println(bool);
        if(bool){
            rmiEmisorServidor rmiES = new rmiEmisorServidor(jTIp.getText());
            boolean tmpbool = rmiES.conexioReceptorServidor();
            if(!tmpbool){
                String msg = "<html>Error en la dirección ip \""+jTIp.getText() +"\" <br>Verifique que posea el componente receptor.</html>";
                JOptionPane.showMessageDialog(jPanel1,msg,"Atencion",JOptionPane.ERROR_MESSAGE,null);
                jTIp.setText(null);
                jBConectar.setEnabled(true);
                jBSalir.setEnabled(true);
                jTIp.setEditable(true);                
            }else{
                Servidor servidor = new Servidor();
                servidor.setIp_servidor(estaIP);
                servidor.setPuerto_servidor("3032");
                rmiES.enviarDatosServidor(servidor);
                verificarjTIp.stop();
                jBConectar.setEnabled(false);
                jLConeccion.setText("Conectado");
                jLConeccion.setIcon(new ImageIcon(getClass().getResource("/4.png")));
                jLConeccion.setToolTipText("Conectado con el servidor CCO.");
            }
        }else{            
            String msg = "<html>Error en la dirección ip \""+jTIp.getText() +"\" <br>Verifique que la ip sea correcta y ademas que el servidor CCO este encendido.</html>";
            JOptionPane.showMessageDialog(jPanel1,msg,"Atencion",JOptionPane.ERROR_MESSAGE,null);            
            jTIp.setText(null);
            jBConectar.setEnabled(true);
            jBSalir.setEnabled(true);
            jTIp.setEditable(true);
        }
    }//GEN-LAST:event_jBConectarActionPerformed

    private void jBSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalirActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jBSalirActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmEmisorServidor().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBConectar;
    private javax.swing.JButton jBSalir;
    private javax.swing.JLabel jLConeccion;
    private javax.swing.JLabel jLDescripcion;
    private javax.swing.JLabel jLEjemploIp;
    private javax.swing.JLabel jLIconoValidador;
    private javax.swing.JLabel jLImagenIp;
    private javax.swing.JLabel jLIp;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTIp;
    // End of variables declaration//GEN-END:variables

}
